<?php

namespace app\api\controller\kuyou\applet;

use app\api\model\kuyou\applet\InventorySerial as InventorySerials;
use app\api\model\kuyou\applet\Goods;
use think\Db;

/**
 * 序列号盘点接口
 */
class InventorySerial extends Common
{
    protected $noNeedRight = ['getdetail','stockList','getSerial'];

    /**
     * 获取序列号盘点
     * @ApiMethod (POST)
     * @param int $page 页码
     * @param int $limit 每页条数
     * @param string $name 序列号盘点名称
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?config('app_limit'):$limit;

            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }

            $sort_field = $this->request->post('sort_field');
            $sort_field = empty($sort_field)?'purchase_code':$sort_field;

            $sort_type = $this->request->post('sort_type');
            $sort_type = empty($sort_type)?'DESC':$sort_type;

            $result = InventorySerials::getList($where,$page,$limit,$sort_field,$sort_type);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取序列号盘点数据
     * @ApiMethod (POST)
     */
    public function stockList()
    {
        if ($this->request->isPost()) {
            $where = [];
            $stores_id = $this->request->post('stores_id');
            if(!empty($stores_id)){
                $where['gs.stores_id'] = $stores_id;
            }
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['gs.goods_code'] = ['like','%'.$goods_code.'%'];
            }
            $goods_type_id = $this->request->post('goods_type_id');
            if(!empty($goods_type_id)){
                $goods_ids = (new Goods())->where(['goods_type_id'=>$goods_type_id])->column('id');
                $where['gs.goods_id'] = ['in',$goods_ids];
            }
            $times = $this->request->post('times');
            if(!empty($times)){
                $str = explode('~',$times);
                if(isset($str[0]) && isset($str[1])){
                    $where['gs.createtime'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $stock = $this->request->post('stock');
            if(!empty($stock)){
                $where['gs.stock'] = '0';
            }
            $status = $this->request->post('status');
            if(empty($status)){
                $where['g.status'] = '1';
            }
            $result = InventorySerials::stockList($where);
            if(isset($result['list'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取序列号盘点详情
     * @ApiMethod (POST)
     * @param int $id 序列号盘点ID
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $purchase_code = $this->request->post('purchase_code');
            if(empty($purchase_code)){
                $this->error('请输入查询参数','',500);
            }
            $where = [];
            $stores_id = $this->request->post('stores_id');
            if(!empty($stores_id)){
                $where['stores_id'] = $stores_id;
            }
            $result = InventorySerials::getDetail($purchase_code,$where);
            if($result['status']==200){
                $this->success('查询成功',$result['msg'],200);
            }else{
                $this->error($result['msg'],'',$result['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 添加序列号盘点
     * @ApiMethod (POST)
     * @param array $detail 序列号盘点数据
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = InventorySerials::addDetail($data);
            if($ret['status']==200){
                $this->success('添加成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 更新序列号盘点
     * @ApiMethod (POST)
     * @param array $detail 序列号盘点数据
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k=>$v){
                $data[$k] = is_array($v)?$v:html_entity_decode($v);
            }
            $ret = InventorySerials::editDetail($data);
            if($ret['status']==200){
                $this->success('更新成功','',200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 删除序列号盘点
     * @ApiMethod (POST)
     * @param int $id 序列号盘点id
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('purchase_code');
            $ret = InventorySerials::delDetail($id);
            if($ret['status']==200){
                $this->success('删除成功',$ret['msg'],200);
            }else{
                $this->error($ret['msg'],'',$ret['status']);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 获取序列号
     * @ApiMethod (POST)
     */
    public function getSerial()
    {
        if ($this->request->isPost()) {
            $data = input();
            $goods_code    = isset($data['goods_code'])?$data['goods_code']:'';
            $stores_id     = isset($data['stores_id'])?$data['stores_id']:'';
            $attribute_id  = isset($data['attribute_id'])?$data['attribute_id']:'';
            $purchase_code = isset($data['purchase_code'])?$data['purchase_code']:'';
            $where = [];
            $where['store_status'] = '1';
            $where['goods_code'] = $goods_code;
            $where['stores_id'] = $stores_id;
            if($purchase_code){
                $where['purchase_code'] = $purchase_code;
            }
            if($attribute_id){
                $where['attribute_id'] = $attribute_id;
            }
            $serial = Db::name('kuyou_goods_serial')->where($where)->field('serial,remark,status')->select();
            $this->success('查询成功',['count'=>sizeof($serial),'list'=>$serial],200);
        }else{
            $this->error('请求方式错误','',250);
        }
    }

    /**
     * 检测序列号
     * @ApiMethod (POST)
     */
    public function period()
    {
        if ($this->request->isPost()) {
            $data = input();
            $goods_code    = isset($data['goods_code'])?$data['goods_code']:'';
            $stores_id     = isset($data['stores_id'])?$data['stores_id']:'';
            $attribute_id  = isset($data['attribute_id'])?$data['attribute_id']:'';
            $where = [];
            $where['store_status'] = '1';
            $where['goods_code'] = $goods_code;
            $where['stores_id'] = $stores_id;
            if($attribute_id){
                $where['attribute_id'] = $attribute_id;
            }
            $serial = Db::name('kuyou_goods_serial')->where($where)->column('serial');
            $serial_arr = [];$serial_data = [];
            $matching = 0;$inventory=0;$loss=0;
            if(isset($data['serial_pds'])){
                $serial_pds = json_decode(html_entity_decode($data['serial_pds']));
                foreach ($serial as $serial_k=>$serial_v){
                    $status = true;
                    foreach ($serial_pds as $serial_pds_k=>$serial_pds_v){
                        if($serial_v==$serial_pds_v){//匹配
                            $matching+=1;
                            $status = false;
                            $serial_arr[] = ['serial'=>$serial_pds_v,'status'=>1];
                        }
                    }
                    if($status){
                        $loss+=1;
                        $serial_arr[] = ['serial'=>$serial_pds_v,'status'=>3];
                    }
                }
                foreach ($serial_pds as $serial_pds_k=>$serial_pds_v){
                    if(!in_array($serial_pds_v,$serial)){
                        $inventory+=1;
                        $serial_arr[] = ['serial'=>$serial_pds_v,'status'=>2];
                    }
                }
                foreach ($serial_pds as $serial_pds_k=>$serial_pds_v){
                    foreach ($serial_arr as $serial_arr_k=>$serial_arr_v){
                        if($serial_arr_v['serial']==$serial_pds_v){
                            $serial_data[] = ['serial'=>$serial_pds_v,'status'=>$serial_arr_v['status']];
                        }
                    }
                }
            }
            $this->success('查询成功',['num'=>($matching+$inventory),'ser'=>($serial_data),'matching'=>$matching,'inventory'=>$inventory,'loss'=>$loss],200);
        }else{
            $this->error('请求方式错误','',250);
        }
    }

}
